package com.deer.wms.inventory.web;

import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.model.stockTake.StockTakeData;
import com.deer.wms.inventory.model.stockTake.StockTakeMaster;
import com.deer.wms.inventory.model.stockTake.StockTakeMasterCriteria;
import com.deer.wms.inventory.model.stockTake.StockTakeTask;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.inventory.service.StockTakeMasterService;
import com.deer.wms.inventory.service.StockTakeTaskService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.review.manage.service.CaseMasterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Created by 郭靖勋 on 2020/02/06.
 */
@Api(description = "盘点单api接口")
@RestController
@RequestMapping("/stock/take/masters")
public class StockTakeMasterController {

    @Autowired
    private StockTakeMasterService stockTakeMasterService;

    @Autowired
    private CaseMasterService caseMasterService;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private StockTakeTaskService stockTakeTaskService;

    @ApiOperation(value = "新增盘点信息", notes = "新增盘点信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody StockTakeData stockTakeData, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        stockTakeMasterService.createStockTakeMaster(stockTakeData,currentUser);
        return ResultGenerator.genSuccessResult();
    }
    
    @GetMapping("/list")
    public Result list(StockTakeMasterCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<StockTakeMaster> list = stockTakeMasterService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        stockTakeMasterService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody StockTakeMaster stockTakeMaster) {
        stockTakeMasterService.updateById(stockTakeMaster);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        StockTakeMaster stockTakeMaster = stockTakeMasterService.getById(id);
        return ResultGenerator.genSuccessResult(stockTakeMaster);
    }

  

    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody StockTakeMasterCriteria criteria) {
        for(Integer id : criteria.getIds()){
            stockTakeMasterService.removeById(id);
        }
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 查询单个盘点单据信息
     * @param billNo
     * @return
     */
    @GetMapping("/findByBillNo")
    public Result findByBillNo(String billNo) {
        StockTakeMaster stockTakeMaster = stockTakeMasterService.getOneByField("bill_no", billNo);
        return ResultGenerator.genSuccessResult(stockTakeMaster);
    }
    
    

    @ApiOperation(value = "审核", notes = "审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @GetMapping("/review")
    public Result review(String billNo, Integer reviewResult,Integer caseDetailId,String memo, @ApiIgnore @User CurrentUser currentUser) {
        StockTakeMaster stockTakeMaster = stockTakeMasterService.findBy("bill_no", billNo);
        //-1不通过   0-流转中  1-通过  2-初始化
        Integer lastReviewResult = caseMasterService.review(caseDetailId,currentUser.getUserId(),reviewResult,memo);


        if (lastReviewResult == -1) {
            stockTakeMaster.setState(-1);
            stockTakeMasterService.updateById(stockTakeMaster);
        } else if (lastReviewResult == 0) {
            stockTakeMaster.setState(0);
            stockTakeMasterService.updateById(stockTakeMaster);
        }else if (lastReviewResult == 2) {
            stockTakeMaster.setState(2);
            stockTakeMasterService.updateById(stockTakeMaster);
        } else if (lastReviewResult == 1) {
            stockTakeMaster.setState(1);
            stockTakeMasterService.updateById(stockTakeMaster);

            // 审核后生成盘点任务
            Integer createUserId = currentUser.getUserId();
            String createUserName = currentUser.getUserName();
            String createTime = DateUtils.getNowDateTimeString();
            stockTakeMasterService.saveStockTakeTasks(stockTakeMaster, billNo, createUserId, createUserName, createTime);

        }
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "重新发起审核", notes = "重新发起审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @GetMapping("/reReview")
    public Result reReview(String billNo, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        StockTakeMaster stockTakeMaster = stockTakeMasterService.findBy("bill_no", billNo);

        //初始化状态 2
        stockTakeMaster.setState(2);
        stockTakeMasterService.updateById(stockTakeMaster);
        caseMasterService.createCase(billNo, 1, currentUser.getUserId(), stockTakeMaster.getWareId(),stockTakeMaster.getOrganizationId());

        return ResultGenerator.genSuccessResult();
    }

   
}